if (!$.cookie('token')) {
    window.location.href = './shopunknwo.html'
}

class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            list: {},
            addressinfo:{},
            id: 0,
            describe: '',
            name: '',
            finishupload: !1,
            uploadword: '上传',
            imglist: [],
            modalcontent:'确定删除吗？',
            score:0,
            detailimg: [],
            detailimglist: [], //存的是base64数组
            swiperimg: [],
            swiperimglist: [],//存的是base64数组
            address:{},
            item:{},
            target:{
                num:'',
                name:''
            },
            // ismodify:!0,
            // determinetrue:0,
            statearr:[
                // {
                //     // state:'ALL',
                //     // content:'全部'
                // },
                {
                    state:'SOLVING',
                    // content:'正在退款处理'
                },
                {
                    state:'SOLVED',
                    // content:'已处理退款'
                },
                {
                    state:'RECEIVED',
                    // content:'已收货'
                },
                {
                    state:'SHIPPED',
                    // content:'已发货'
                },
                {
                    state:'CANCELED',
                    // content:'已取消'
                },
                {
                    state:'PAY_SUCCESS',
                    // content:'已支付'
                },
                {
                    state:'NO_PAY',
                    // content:'未支付'
                },
                ]
        };
    }

    clickupload(e) {
        console.log(e, 'clickupload');
        this.setState({
            filecode: e
        }, () => { $('#file').click(); })

    }




  



    submit() {
        let {item,newstate}=this.state
        
            var data = { ordernumber:item.ordernum,state:newstate }
            changesorder(data, res => {
                if (res.issuccess == 1) {
                    alert("保存成功")
                    // if(newstate=='SHIPPED'){
                    //     this.setState({ismodify:!1})
                    // }
                    this.freshlist()
                }else{
                    alert(res.msg)
                }
            })
        
        // var { thumburl, describe, cid, price, color, state, name, swiperimglist, detailimglist, finishupload } = this.state
        //     , err = ''

        // //   if(detailimglist.length==0) err = '请添加详情图片';
        // //   if(swiperimglist.length==0) err = '请添加轮播图片';
        // if (thumburl.length == 0) err = '请添加头图';
        // if (describe.length == 0) err = '请填写描述';
        // if (!finishupload) err = '请上传图片';
        // console.log(color, 'color');

        // if (color.length == 0) err = '请填写颜色';
        // // if(Number(inventory)<=0||isNaN(inventory)) err = '请正确填写库存';
        // if (Number(price) <= 0 || isNaN(price)) err = '请正确填写价格';
        // if (Number(score) <= 0 || isNaN(score)) err = '请正确填写积分';
        // // if(Number(postamount)<0||isNaN(postamount)) err = '请正确填写包邮额度';
        // // if(Number(postfee)<0||isNaN(postfee)) err = '请正确填写邮费';
        // if (name.length == 0) err = '请填写姓名';
        // if (err.length > 0) {
        //     alert(err);
        //     return;
        // }
        // swiperimglist = swiperimglist.join(',');
        // detailimglist = detailimglist.join(',')




        // //   if(describe.length==0) err = '物品描述不能为空';
        // //   if(thumbPicPath.length==0) err = '请上传图片';
        // //   if(isNaN(inventory)||Number(inventory)<=0) err = '库存有误';
        // //   if(isNaN(price)||Number(price)<=0) err = '单价有误';
        // //   if(name.length==0) err = '姓名不能为空';
        // //   if(err.length>0) {alert(err);return}
        // var submitdata = {
        //     thumbPicPath: thumburl, state, cid, describe, name, price, color, swiperimglist
        //     , detailimglist
        // }
        // if (this.state.id > 0) submitdata['id'] = this.state.id;
        // addgoods(submitdata, res => {
        //     if (res.issuccess == 1) {
        //         alert(`${this.state.id > 0 ? '修改' : '保存'}成功`);
        //         // window.location.href='./main.html'
        //     }
        // })
    }



    componentDidMount() {
        this.freshlist()
    }
    freshlist(){
        var number = GetQueryString("number"),that = this;
        if(number=='null'){
            alert('订单号不能为空');
            window.location.href='./order.html'
        }
        // let isback = 1;
        findbynumber(number,res=>{
            if(res.issuccess=='1'){
                // console.log(res.data,'list');

                that.setdata(res.data);
                // that.setState({list:res.data.books})
            }
        })
    }

    setdata(list){
        // console.log(list,'list');
        // let goodsinfo = JSON.parse(decodeurl(list.goodsinfo))
        // ,goodshtml=[]
        // ,addressinfo=JSON.parse(decodeurl(list._address));
        // console.log(list,'list');
        // console.log(addressinfo,'addressinfo');
        let goodshtml=[],{address,item,books}=list, statehtml = [];
        this.state.statearr.forEach((x,i)=>{
            statehtml.push(
                <option key={i} value={x.state}>{getstatecontent(x.state)}
                                </option>
            )
        })
        this.setState({statehtml},()=>{
            $(`option[value=${item.state}]`).prop("selected","selected");
        })
        books.forEach(x=>{
            goodshtml.push(
                <div className='item'>
                    <img src={`${API_HEAD}imgs/${x.detaillist}`}/>
                    <p>{x.name}</p>
                </div>
            )
        })
        if(item.express_num||item.express_name){
            let targetitem={num:item.express_num,name:item.express_name}
            this.setState({target:targetitem})
        }
        this.setState({goodshtml,list:books,address,item,state:item.state})
    }

    inputchange(prop, e) {
        let { target } = this.state;
        target[prop] = e.target.value
        this.setState({ target })
        // console.log(e, 'e');
        // console.log(x, 'x');
        // var prop = e == 0 ? 'name' : e == 1 ? "price" : e == 2 ? "postfee" : e == 3 ? "describe" : e == 4 ? "color" :e==6?'score':'postamount'
        // this.setState({
        //     [prop]: x.target.value
        // })
    }
    // statechange(e) {
    //     console.log(e);
    //     this.setState({
    //         state: e
    //     })

    // }
    oncouponchange(e){
        console.log(e.target.value,'oncouponchange');
        
        this.setState({ cancoupon: e.target.value })
    }
    oncategorychange(e) {
        console.log(e.target.value, 'changegg');
        this.setState({ cid: e.target.value })
    }
    refund() {
        this.setState({modalcontent:'确定要退款吗？',modaltype:'refund'},()=>{
            
            $('#myModal').modal('toggle');
        })
    }
    sendgoods() {
        this.setState({modalcontent:'确定要发货吗？',modaltype:'send'},()=>{
            
            $('#myModal').modal('toggle');
        })
    }
    back(){
        window.location.href='./order.html'
    }
    confirmdel() {
        let {modaltype,list} = this.state;
        console.log(modaltype,"modaltype");
        if(modaltype=='refund'){//发起退款
            requestfund(list.orderNumber,list.realAmount,res=>{
                
            })
        }

        if(modaltype=='send'){//发货
            sendgoods(list.orderNumber,res=>{
                if(res.issuccess==1){
                    $('#myModal').modal('toggle');
                    alert('发货成功');
                    window.location.href='../order.html'
                }
            })
        }
        
        // delgoods(this.state.id, res => {
        //     if (res.issuccess == 1) {
        //         window.location.href = './main.html'
        //     }
        // })
    }
    onstatechange(e){
        let newstate=e.target.value
        // console.log(e.target.value,'onstatechange');
        //  this.freshlist(0,e.target.value)
         this.setState({
            newstate
         })
     
     }
    //  修改快递
    //  modify(){
    //     this.setState({ismodify:!1})
    //  }
    //  填写物流信息
     determine(){
        let {target,item,state}=this.state,{num,name}=target,err=""
        if(name.length==0)err="请输入快递公司名称"
        if(num.length==0)err="请输入快递单号"
        if (err.length > 0) {
            alert(err);
            return
        }
        var data = { ex_name:name, ex_num:num,ordernumber:item.ordernum }
        setexpress(data,res=>{
            if(res.issuccess==1){
                alert("填写成功")
                this.freshlist()
                // this.setState({determinetrue:1})
            }else{
                alert(res.msg)
            }
        })      
     }
    render() {
        let {goodshtml,item,address,addressinfo,modalcontent,target,statehtml,ismodify } =this.state;
        // console.log(list,'list');
        

        //  <img style={{display:this.state.thumburl.length>0?"block":"none"}} src={this.state.thumburl} className="img-responsive"  /> 

        return (
            <div>
                <Modal content={modalcontent} click={this.confirmdel.bind(this)} />
                <div className='content_wrap panel-default content_wrap width'>
                    <div className='breadLine '>
                        当前位置 > 订单详情
                    </div>
                    <div className='inputs panel-body'>
                        <h3>订单详情</h3>

                        <div className="input-group input-group">
                            <span className="input-group-addon ">订单号</span>
                            <p>{item.ordernum}</p>
                        </div>

                        {/* <div style={{display:item.isscore==1?"":"none"}} className="input-group input-group">
                            <span className="input-group-addon ">消耗积分</span>
                            <p>{item.goodsid}</p>
                        </div> */}
                    <div style={{display:item.isscore=='1'?"none":""}} className="input-group input-group">
                            <span className="input-group-addon ">应付金额</span>
                            <p>￥{item.total/100}</p>
                        </div>
                        <div style={{display:item.isscore=='1'?"none":""}} className="input-group input-group">
                            <span className="input-group-addon ">实付金额</span>
                            
                            <p>{item.cost?"￥"+item.cost/100:"未支付"}</p>
                        </div>
                        <div style={{display:item.isscore=='1'?"none":""}} className="input-group input-group">
                            <span className="input-group-addon ">押金</span>
                            <p>￥{item.deposit/100}</p>
                        </div>

                        <div className="input-group input-group">
                            <span className="input-group-addon ">订单状态 </span>
                            <select value={this.state.cid} onChange={this.onstatechange.bind(this)} className="form-control select">
                                {statehtml}
                            </select>
                            {/* <p>{getstatecontent(item.state)}</p> */}
                        </div>
                        <div className="input-group input-group">
                            <span className="input-group-addon ">订单生成</span>
                            <p>{formatDate(new Date(item.createDate))}</p>
                        </div>
                        <div style={{display:item.payDate=='0001-01-01T00:00:00'?"none":""}} className="input-group input-group">
                        {/* <div className="input-group input-group"> */}
                            <span className="input-group-addon ">交易时间</span>
                            <p>{formatDate(new Date(item.payDate))}</p>
                        </div>

                        {/* <div className="input-group input-group">
                            <span className="input-group-addon ">书单</span>
                            <p>{item.isscore=='0'?"普通":'积分'}商品</p>
                        </div> */}

                        {/* <div style={{display:item.iscore?"":"none"}} className="input-group input-group">
                            <span className="input-group-addon ">积分</span>
                            <p>{item.score}分</p>
                        </div> */}
                      
                        <div className="input-group">
                            <span className="input-group-addon">书单</span>
                            <div className='goodslist'>
                            {goodshtml}
                            </div>
                        </div>
                        <div className="input-group input-group">
                            <span className="input-group-addon ">地址信息</span>
                            <div className='address'>
                            <p>姓名：{address.username}</p>
                            <p>电话：{address.phone}</p>
                            <p>联系地址：{address.province+address.city+address.countyName+address.info}</p>
                            </div>
                        </div>
                        <div style={{ display: item.state == 'SHIPPED' ? "none" : "" }}>
                        <div className="input-group input-group">
                            <span className="input-group-addon ">快递单号</span>
                            <p>{item.express_num?item.express_num:'暂无'}</p>
                        </div>
                        <div className="input-group input-group">
                            <span className="input-group-addon ">快递名称</span>
                            <p>{item.express_name?item.express_name:'暂无'}</p>
                        </div>
                        </div>
                        {/* <div style={{ display: ismodify? "" : "none"  }}> */}
                        <div style={{ display: item.state == 'SHIPPED' ? "" : "none" }}>
                            <div className="input-group">
                                <span className="input-group-addon">快递单号</span>
                                <input id='name' onChange={this.inputchange.bind(this, 'num')} value={target.num} type="text" placeholder='请输入快递单号' className="form-control" />
                            </div>
                            <div className="input-group">
                                <span className="input-group-addon">快递名称</span>
                                <input id='name' onChange={this.inputchange.bind(this, 'name')} value={target.name} type="text" placeholder='请输入快递公司名称' className="form-control" />
                            </div>
                        <div  className="btns_bottom center margintop">
                            <button className='btn add btn-primary center w20' onClick={this.determine.bind(this)} >保存物流信息</button>
                        </div>
                        </div>
                      
                      

                        <div className='btns_bottom center'>
                            {/* <button className='btn add btn-primary center w20' onClick={this.modify.bind(this)} >填写物流信息</button> */}
                            <button className='btn add btn-primary center w20' onClick={this.submit.bind(this)} >保存</button>
                            <button onClick={this.refund.bind(this)} style={{ display: item.state == 'SOLVING' ? "" : "none" }} className='btn add btn-danger center w20' >发起退货</button>
                            <button onClick={this.sendgoods.bind(this)} style={{ display: item.state == 'PAY_SUCCESS' ? "" : "none" }} className='btn add btn-success center w20' >发货</button>
                            <button className='btn add btn-primary center w20' onClick={this.back.bind(this)} id='btnimg_save'>返回</button>
                        </div>
                    </div>
                </div>
            </div>

        )
    }
}


ReactDOM.render(
    <div>
        <TopLine />
        {/* <LeftMenu code='6' /> */}
        <LeftMenu code='order' />
        <Main />
    </div>
    , $('.container')[0]
)